package com.ykdl.app.common;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.helpers.NamespaceSupport;

import android.app.Application;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

import com.litesuits.http.LiteHttp;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.data.NameValuePair;
import com.litesuits.http.request.param.HttpMethods;
import com.ykdl.app.Utils.AccessTokenKeeper;
import com.ykdl.app.Utils.MobileUtils;
import com.ykdl.app.base.QLog;
import com.ykdl.app.prefs.MyPrefs;
public class MyApplication extends Application {
	
	private static MyApplication instance = null;
	public static MyApplication getInstance(){
		return instance;
	}
	private HttpConfig config ;
	private LiteHttp liteHttp;
	public LiteHttp getLiteHttp() {
		return liteHttp;
	}

	@Override
	public void onCreate() {
		instance = this;
		MyPrefs.init(this);
		initHttpUtil();
		initJPuth();
		super.onCreate();
	}
	
	public void initHttpUtil(){
		AccessTokenKeeper mTokenKeeper = new AccessTokenKeeper(instance);
		config =  new HttpConfig(instance);
		config.setTimeOut(100000, 10000);
		config.setUserAgent("Mozilla/5.0 (...)");
//		NameValuePair bean = new NameValuePair("access_token","09e8a57bb2401a66b143c0dabd3868f04e7f0e52");
		NameValuePair bean = new NameValuePair(mTokenKeeper.getmAccessTokenTitle(),
				mTokenKeeper.getmAccessToken());
		NameValuePair bean2 = new NameValuePair("platform", "android");
		NameValuePair bean3 = new NameValuePair("ver", MobileUtils.getVersion()+"");
		NameValuePair bean4 = new NameValuePair("deviceid", MobileUtils.getDeviceID());
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(bean);
		list.add(bean2);
		list.add(bean3);
		list.add(bean4);
		config.setCommonHeaders(list);
		liteHttp = LiteHttp.newApacheHttpClient(config);
		liteHttp.setDebugged(true);
		
		
	}
	public  void syncAccessToken() {
		AccessTokenKeeper mTokenKeeper = new AccessTokenKeeper(instance);
		NameValuePair bean = new NameValuePair(mTokenKeeper.getmAccessTokenTitle(),
				mTokenKeeper.getmAccessToken());
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		NameValuePair bean2 = new NameValuePair("platform", "android");
		NameValuePair bean3 = new NameValuePair("ver", MobileUtils.getVersion()+"");
		NameValuePair bean4 = new NameValuePair("deviceid", MobileUtils.getDeviceID());
		list.add(bean);
		list.add(bean2);
		list.add(bean3);
		list.add(bean4);
		HttpConfig config =  new HttpConfig(instance);
		config.setCommonHeaders(list);
		liteHttp = LiteHttp.newApacheHttpClient(config);
		liteHttp.setDebugged(true);
	}
	/**
	 * 清除LiteHttp中的Token
	 */
	public void clearAccessToken(){
		List<NameValuePair> commonHeaders = new ArrayList<NameValuePair>();
		config.setCommonHeaders(commonHeaders);
		liteHttp = LiteHttp.newApacheHttpClient(config);
		liteHttp.setDebugged(true);
	}
	/**
	 * 初始化极光推送
	 */
	private void initJPuth(){
		JPushInterface.init(instance);
		JPushInterface .setDebugMode(true);
	}
}
